package com.hzncc.flowable_diboot.trial_contracts.controller;

import com.diboot.core.controller.BaseCrudRestController;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Pagination;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.diboot.iam.annotation.OperationCons;
import com.hzncc.flowable_diboot.trial_contracts.dto.TrialContractAuditDTO;
import com.hzncc.flowable_diboot.trial_contracts.entity.TrialContract;
import com.hzncc.flowable_diboot.trial_contracts.service.TrialContractService;
import com.hzncc.flowable_diboot.trial_contracts.vo.TrialContractAuditDetailVO;
import com.hzncc.flowable_diboot.trial_contracts.vo.TrialContractAuditListVO;
import com.hzncc.flowable_diboot.trial_contracts.vo.TrialContractDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 试用合同 相关rest接口
 *
 * @author MyName
 * @version 1.0
 * @date 2024-10-15
 * Copyright © MyCorp
 */
@Slf4j
@RequestMapping("/trial-contract/trial-contract")
@BindPermission(name = "试用合同")
@RestController
public class TrialContractController extends BaseCrudRestController<TrialContract> {

    @Autowired
    private TrialContractService trialContractService;

    /**
     * 查询资源对象的列表VO记录
     * <p>
     * url请求参数示例: ?fieldA=abc&pageSize=20&pageIndex=1&orderBy=id
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping()
    public JsonResult<List<TrialContractAuditListVO>> getListVOMapping(TrialContractAuditDTO queryDto, Pagination pagination, String type, String permission) throws Exception {
        List<TrialContractAuditListVO> trialContractAuditListVOS = trialContractService.selectContractList(queryDto, pagination, type, permission);
        return JsonResult.OK(trialContractAuditListVOS).bindPagination(pagination);
    }

    /**
     * 根据id查询资源对象的详情VO
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult<TrialContractDetailVO> getDetailVOMapping(@PathVariable("id") String id) throws Exception {
        TrialContractDetailVO detailVO = trialContractService.getViewObject(id, TrialContractDetailVO.class);
        return JsonResult.OK(detailVO);
    }


    /**
     * 创建资源对象数据
     *
     * @param trialContract
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping()
    public JsonResult<?> createModelMapping(@RequestBody TrialContract trialContract) throws Exception {
        boolean success = trialContractService.createEntity(trialContract);
        log.debug("新建数据 试用合同:{} {}", trialContract.getId(), success ? "成功" : "失败");
        return JsonResult.OK(trialContract.getId());
    }

    /**
     * 批量创建资源对象数据
     *
     * @param trialContractList
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_BATCH_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch")
    public JsonResult<?> batchCreateModelMapping(@RequestBody List<TrialContract> trialContractList) throws Exception {
        boolean success = trialContractService.createEntities(trialContractList);
        log.debug("批量新建数据 试用合同 {}", success ? "成功" : "失败");
        return JsonResult.OK(success);
    }

    /**
     * 根据id更新资源对象
     *
     * @param trialContract
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult<?> updateModelMapping(@PathVariable("id") String id, @RequestBody TrialContract trialContract) throws Exception {
        boolean success = trialContractService.updateEntity(trialContract);
        log.debug("更新数据 试用合同:{} {}", id, success ? "成功" : "失败");
        return JsonResult.OK(success);
    }

    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult<?> deleteModelMapping(@PathVariable("id") String id) throws Exception {
//        boolean success = trialContractService.deleteEntity(id);
//        log.debug("删除数据 试用合同:{} {}", id, success ? "成功" : "失败");
//        return JsonResult.OK(success);
        return trialContractService.deleteContract(Collections.singletonList(id));
    }

    /**
     * 根据ids集合删除资源对象
     *
     * @param ids
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_BATCH_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch-delete")
    public JsonResult<?> deleteEntityMapping(@RequestBody List<String> ids) throws Exception {
//        boolean success = trialContractService.deleteEntities(ids);
//        log.debug("批量删除数据 试用合同:{} {}", ids, success ? "成功" : "失败");
//        return JsonResult.OK(success);
        return trialContractService.deleteContract(ids);
    }

    /**
     * 保存合同草稿
     *
     * @param contractDetail
     * @return
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/save_contract")
    public JsonResult<?> saveContract(@RequestBody TrialContractAuditDetailVO contractDetail) {
        return JsonResult.OK(trialContractService.saveTrialContract(contractDetail, false));
    }


    /**
     * 提交合同
     *
     * @param contractDetail
     * @return
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/submit_contract")
    public JsonResult<?> submitContract(@RequestBody TrialContractAuditDetailVO contractDetail) {
        return JsonResult.OK(trialContractService.saveTrialContract(contractDetail, true));
    }

    /**
     * 暂存合同变更
     *
     * @param contractDetail
     * @return
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/save_update")
    public JsonResult<?> saveUpdateContract(@RequestBody TrialContractAuditDetailVO contractDetail) {
        return JsonResult.OK(trialContractService.updateTrialContract(contractDetail, false));
    }

    /**
     * 提交合同变更
     *
     * @param contractDetail
     * @return
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/submit_update")
    public JsonResult<?> submitUpdateContract(@RequestBody TrialContractAuditDetailVO contractDetail) {
        return JsonResult.OK(trialContractService.updateTrialContract(contractDetail, true));
    }

    /**
     * 暂存合同取消
     *
     * @param contractDetail
     * @return
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/save_cancel")
    public JsonResult<?> saveCancelContract(@RequestBody TrialContractAuditDetailVO contractDetail) {
        return JsonResult.OK(trialContractService.cancelTrialContract(contractDetail, false));
    }

    /**
     * 提交合同取消
     *
     * @param contractDetail
     * @return
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/submit_cancel")
    public JsonResult<?> submitCancelContract(@RequestBody TrialContractAuditDetailVO contractDetail) {
        return JsonResult.OK(trialContractService.cancelTrialContract(contractDetail, false));
    }

    /**
     * 获取暂存合同
     *
     * @param id
     * @return
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/updateSaved/{id}")
    public JsonResult<?> getUpdateSavedContract(@PathVariable String id) {
        TrialContractAuditDetailVO updateSavedContract = trialContractService.getUpdateSavedContract(id);
        return JsonResult.OK(updateSavedContract);
    }

    /**
     * 合同确认
     *
     * @param id
     * @return
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @GetMapping("/contract_confirmation/{id}")
    public JsonResult<?> contractConfirmation(@PathVariable String id) {
        return null;
    }

    /**
     * 合同历史记录
     *
     * @param queryDto
     * @param pagination
     * @return
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/history")
    public JsonResult<?> contractHistory(TrialContractAuditDTO queryDto, Pagination pagination) {
        List<TrialContractAuditListVO> contractHistory = trialContractService.getContractHistory(queryDto, pagination);
        return JsonResult.OK(contractHistory);
    }

}